﻿/**
 * @Description 基础框架服务调用实例-资源树服务
 *@Author  guoyanfei
 */
var resourcesTree = resourcesTree || {};
var serviceUrl = '/gdp'
resourcesTree.prototype = {
    init: function () {//初始化
    	resourcesTree.prototype.setSearchColumn();
        resourcesTree.prototype.createTree();
        resourcesTree.prototype.getSearchOption();//查询操作
    },
    createTree:function(){//创建树
        var result = false;
        $.ajax({
            url: serviceUrl + "/business/execute/lodingorno",
            type: 'post',
            data: { serviceID: serviceId },
            async: false,
            success: function (data) {
                result = data;
            }
        });
        if (result) {
            resourcesTree.prototype.getLazyTreeData();
        } else {
            resourcesTree.prototype.getAllTreeData();
        }
    },
    getAllTreeData: function () {//获取所有资源树数据
        var setting = {
            async: {
                enable: true,
                url: serviceUrl + "/business/execute/selectTreeConfig",
                otherParam:{serviceID:serviceId,conQuery:resourcesTree.prototype.setSearchParams()},
                type:"get",
                dataFilter:function(treeId,parentNode,responseData){
                	return responseData.data;
                }
            }
        };
        $.fn.zTree.init($("#tree"), setting);
    },
    getLazyTreeData: function () {//懒加载数据
        var setting = {
            async: {
                enable: true,
                autoParam: ["id", "grade"],
                url: serviceUrl + "/business/execute/getPartTreeConfig",
                otherParam:{serviceID:serviceId,conQuery:resourcesTree.prototype.setSearchParams()},
                type: "get",
                dataFilter:function(treeId,parentNode,responseData){
                	return responseData.data;
                }
            },
            view:{
            	addHoverDom: addHoverDom,
            	removeHoverDom: removeHoverDom
            }
        };
        function addHoverDom(treeId, treeNode){
        	var aObj = $("#" + treeNode.tId + "_a");
        	if($('#diyBtn_'+treeNode.id).length <=0){
        		var editStr = "<span id='diyBtn_" +treeNode.id+ "'class='diyremove' style='margin-left:30px; border:solid 1px blue;'>其他字段信息:"+JSON.stringify(treeNode.otherfield)+"</span>";
        	}
			aObj.after(editStr);
        }
        function removeHoverDom(treeId, treeNode){
        	$(".diyremove").unbind().remove();
        }
        
        $.fn.zTree.init($("#lazytree"), setting);
        
    },
    setSearchColumn:function(){//设置查询列
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/execute/selectTreeColumn',
            type: 'post',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("查询列数据加载失败!");
            }
        });
        if (columnData != null) {
            $.each(columnData, function (i, e) {
            	var fieldname = "search"+e.fieldname;
            	fieldname = fieldname.replace("@","_");
                switch (e.showtype) {
                    case "0"://文本框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='" + fieldname + "' placeholder='请输入" + e.fieldnamecn + "' type='text' class='form-control' >");
                        break;
                    case "1"://下拉框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        var strtxt = "<select id='" + fieldname + "'  class='form-control' >"
                                       + "<option value=''>请选择</option>";
                        if (e.enumvalue != null && e.enumvalue != "") {
                            var enums = e.enumvalue.split(',');
                            for (var i = 0; i < enums.length; i++) {
                                var eb = enums[i].split(':');
                                strtxt += "<option value='"+eb[0]+"'>"+eb[1]+"</option>";
                            }
                        }
                        strtxt += "</select>";
                        $("#txtSearch").append(strtxt);
                        break;
                    case "4"://日期框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='" + fieldname + "' data-date-format=\"yyyy-mm-dd\" type='text' class='form-control' >");
                        $("#" + fieldname + "").datetimepicker();
                        break;
                }
            });
            
        }
    },
    setSearchParams:function(){//设置查询参数
        var searchParams = "";
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/execute/selectTreeColumn',
            type: 'post',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("查询列数据加载失败!");
            }
        });
        if (columnData != null) {
            $.each(columnData, function (i, e) {
            	var fieldname = "search"+e.fieldname;
            	fieldname = fieldname.replace("@","_");
                if ($('#' + fieldname).val() != null && $('#' + fieldname).val() != 'undefined' && $('#' + fieldname).val() != "") {
                	  if (searchParams == "") {
                          searchParams = "\"" + e.fieldname + "\":\""+ $('#' + fieldname).val()+"\"";
                      } else { 
                          searchParams += ",\"" + e.fieldname + "\":\"" + $('#' + fieldname).val() + "\"";
                      }
                }
            });
            searchParams = "{" + searchParams + "}";
        }
        return searchParams;
    },
    getSearchOption:function(){
    	$("#btn_search").click(function(){
    		resourcesTree.prototype.createTree();
    	});
    }
};
//服务初始化
$(function () {
    $("#sp_seviceUrl").append("服务地址：" + serviceUrl + "/business/execute/selectTreeConfig");
    $("#sp_seviceUrl").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务ID：" + serviceId);

    resourcesTree.prototype.init();
    
})
